package com.example.demo.controller;

import com.example.demo.annoation.Anonymous;
import com.example.demo.util.RabbitMQTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
public class TestController extends BaseAdminController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private RabbitMQTool rabbitMQUtil;

    @Anonymous
    @GetMapping("/testRabbitMQSend")
    public void testRabbitMQSend() {
        String message = "周鹏大帅哥";
        String queueName = "simple.queue";
        rabbitTemplate.convertAndSend(queueName, message);
    }

    @Anonymous
    @GetMapping("/testWorkQueue")
    public void testWorkQueue() throws InterruptedException {
        for (int i = 0; i <= 50; i++) {
            String message = "周鹏大帅哥,message_" + i;
            String queueName = "work.queue";
            rabbitTemplate.convertAndSend(queueName, message);
            Thread.sleep(20);
        }
    }

    @Anonymous
    @GetMapping("/testSendFanout")
    public void testSendFanout() {
        String message = "周鹏大帅哥";
        String exchangeName = "test.fanout";
        rabbitTemplate.convertAndSend(exchangeName, null, message);
    }

    @Anonymous
    @GetMapping("/testSendDirect")
    public void testSendDirect() {
        String message = "红色，周鹏大帅哥";
        String exchangeName = "test.direct";
        rabbitTemplate.convertAndSend(exchangeName, "blue", message);
    }

    @Anonymous
    @GetMapping("/testSendTopic")
    public void testSendTopic() {

//        // 声明死信交换机和真实消费队列并绑定
//        DirectExchange dlx = rabbitMQUtil.declareDirectExchange("ex.dlx.task");
//        Queue realQueue = rabbitMQUtil.declareQueue("queue.task.real", true);
//        rabbitMQUtil.bindDirect(realQueue, dlx, "task.created");
//
//        // 声明延时队列，过期后路由到上面的 DLX+RK
//        rabbitMQUtil.declareDelayQueueWithDLX(
//                "queue.task.delay",
//                "ex.dlx.task",
//                "task.created",
//                true,
//                null // 不设置统一 TTL，使用每条消息单独 TTL
//        );
//
//        // 发送延时 10s 的消息（进入延时队列，过期后转发到真实队列）
//        rabbitMQUtil.sendToDelayQueue("queue.task.delay", Map.of("id", 2), 10_000L);

        // 声明延时交换机（底层 direct/topic/fanout 均可）
        CustomExchange delayedEx = rabbitMQUtil.declareDelayedExchange("ex.delay.plugin", "direct");
        // 声明并绑定队列
        Queue taskQueue = rabbitMQUtil.declareQueue("queue.delay.plugin.task", true);
        rabbitMQUtil.bindDelayed(taskQueue, delayedEx, "task.created");
        // 发送延时 5s 的消息
        rabbitMQUtil.sendToDelayedExchange("ex.delay.plugin", "task.created", Map.of("id", 1), 10000L);
    }
}
